import { ref } from 'vue'
import type { AnswerTopicIndex } from '@/api/constants'
import type { ExamQuestionAnswerVo, ExamQuestionCollectVo } from '@/api/exam/types'
import type { LearnBaseQuestionInfo, SelectedBase, Know, KnowExercise } from '@/api/base/types'
import { ExerciseTypeEnum } from '@/api/constants'
import { useQuestions, useSelectedBase, useBaseInfo } from './LearnBase'
import { useLearnBaseInfoStore } from '@/stores/modules/learn_base_info'
import { useExericseStore } from '@/stores/modules/exercise_store'
import { useSignsStore } from '@/stores/modules/signs_store'

import { formatDate } from '@/utils/index'

/**
 * 获取考点总结试题
 */
const getKnowExerciseQuestions = (knowSummaryId: number) => {
  let learnBaseInfoStore = useLearnBaseInfoStore()
  let baseInfo = useBaseInfo()
  let knowSummaryQuestionIds = baseInfo.knowSummaries?.find(item => item.id == knowSummaryId)?.questionIds
  let questions = knowSummaryQuestionIds?.map(questionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId == questionId)!
  })

  return questions
}
/**
 * 获取标志的试题
 * @param baseType
 * @param signParentId
 * @param signId
 */
export const useSignQuestions = (baseType: string, signParentId: number, signId: number) => {
  let signsStore = useSignsStore()
  let learnBaseInfoStore = useLearnBaseInfoStore()

  let signQuestionIds = signsStore.signs?.find(item => item.id == signParentId)?.children?.find(item => item.id == signId)?.questions
  let questions = signQuestionIds?.map(questionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId == questionId)!
  })
  questions = questions?.filter(item => item?.baseTypeCode === baseType)
  return questions
}

/**
 * 清除收藏题
 * @param baseType
 */
export const useClearCollectQuestion = (baseType: string) => {
  const exericseStore = useExericseStore()
  let collectQuestions = exericseStore.questionCollects.filter((item) => item.baseTypeCode != baseType)
  exericseStore.questionCollects = collectQuestions
}

/**
 *收藏试题-统计
 * @param baseType
 */
export const useCollectQuestionOver = (baseType: string) => {
  let allCollectQuestionCount = 0
  let todayCollectQuestionCount = 0

  let allCollectQuestions = getSequenceExerciseCollectsQuestions(baseType)
  allCollectQuestionCount = allCollectQuestions.length
  let todayCollectQuestions = getTodayCollectQuestions(baseType)
  todayCollectQuestionCount = todayCollectQuestions.length

  return {
    allCollectQuestionCount, todayCollectQuestionCount
  }
}
/**
 * 今日收藏题
 * @param baseType
 * @returns
 */
const getTodayCollectQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  let date = formatDate(new Date(), 'YYYY-MM-DD')
  let dateBegin = date + ' 00:00:00'
  let dateEnd = date + ' 23:59:59'

  let collectQuestionIds = exericseStore.questionCollects.filter(item => item.creatTime! >= dateBegin && item.creatTime! <= dateEnd && item.questionId)
  let questions = collectQuestionIds.map(collectQuestionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === collectQuestionId)!
  })

  return questions
}
/**
 * 清除错题
 * @param baseType
 */
export const useClearErrorQuestion = (baseType: string) => {
  const exericseStore = useExericseStore()
  let answerCorrectQuestions = exericseStore.questionAnswerErrors.filter((item) => item.baseTypeCode === baseType && item.status === 1)
  exericseStore.questionAnswerErrors = answerCorrectQuestions
}
/**
 *错题统计
 * @param baseType
 */
export const useErrorQuestionOver = (baseType: string) => {
  let allErrorQuestionCount = 0
  let todayErrorQuestionCount = 0
  let correctRate = 0
  let answeredCount = useQuestionAnswerCount(baseType).count

  let allErrorQuestions = getSequenceExerciseErrorQuestions(baseType)
  allErrorQuestionCount = allErrorQuestions.length

  let todayErrorQuestions = getTodayErrorQuestions(baseType)
  todayErrorQuestionCount = todayErrorQuestions.length
  correctRate = useAnswerCorrectRate(baseType)

  return {
    answeredCount, allErrorQuestionCount, todayErrorQuestionCount, correctRate
  }
}

/**
 * 考点练习进度总览
 * @param baseType
 */
export const useKnowExerciseProgressOver = (baseType: string) => {
  const exericseStore = useExericseStore()
  let knowsExerciseProgress = exericseStore.knowsExerciseProgress.filter(item => item.baseTypeCode == baseType)
  let noExerciseCount = knowsExerciseProgress.filter(item => item.skilled == 0).length
  let noSkilledCount = knowsExerciseProgress.filter(item => item.skilled == 1).length
  let skilledCount = knowsExerciseProgress.filter(item => item.skilled == 2).length
  return { noExerciseCount, noSkilledCount, skilledCount }

}
/**
 * 获取考点练习进度列表
 */
export const useGetKnowExerciseProgress = (baseType: string, knowTypeCode: string) => {
  const exericseStore = useExericseStore()
  let knowsExerciseProgress = exericseStore.knowsExerciseProgress.filter(item => item.baseTypeCode == baseType && item.knowTypeCode == knowTypeCode)
  return knowsExerciseProgress
}

/**
 * 初始化考点练习
 * @param knowExercises
 */
export const useInitKnowsExerciseProgress = (knowExercises: KnowExercise[]) => {
  initKnowsExerciseProgressByBaseType(knowExercises, 'km1')
  initKnowsExerciseProgressByBaseType(knowExercises, 'km4')
}
/**
 * 根据科目初始化考点进度
 * @param knowExercises
 * @param baseType
 */
const initKnowsExerciseProgressByBaseType = (knowExercises: KnowExercise[], baseType: string) => {
  const exericseStore = useExericseStore()
  const learnBaseInfoStore = useLearnBaseInfoStore()

  let baseTypeQuestionIds = learnBaseInfoStore.questions(baseType).map((item: LearnBaseQuestionInfo) => item.questionId)
  let knowsExerciseProgress = exericseStore.knowsExerciseProgress

  let allKnows: Know[] = []
  knowExercises = knowExercises.filter(item => item.baseTypeCode?.includes(baseType))
  knowExercises.forEach(item => {
    allKnows = [...allKnows, ...item.knows!]
  })

  allKnows.forEach(know => {
    let tempKnow = { ...know }
    tempKnow.baseTypeCode = baseType
    tempKnow.questions = tempKnow.questions?.filter(item => baseTypeQuestionIds.includes(item))
    if (tempKnow.questions!.length >= 1) {
      let knowExerciseProgress = knowsExerciseProgress.find(knowExerciseProgress => knowExerciseProgress.id == know.id && knowExerciseProgress.baseTypeCode === baseType)

      if (!knowExerciseProgress) {
        knowsExerciseProgress.push({ ...tempKnow, ...{ questionAnswerCount: 0, questionAnswerCorrectCount: 0, questionAnswerErrorCount: 0, answerCorrectRate: 0, skilled: 0 } })
      } else {
        knowExerciseProgress.questions = tempKnow.questions
        // 再设置下考点练习进度
        setKnowExerciseProgress(tempKnow.baseTypeCode!, [tempKnow])
      }
    }
  })
}
/**
 *设置考点练习进度
 * @param knows
 */
const setKnowExerciseProgress = (baseType: string, knows: Know[] | undefined) => {
  if (!knows) return
  const exericseStore = useExericseStore()
  let knowsExerciseProgress = exericseStore.knowsExerciseProgress

  knows.forEach(know => {

    know.questions = knowsExerciseProgress.find(item => item.id == know.id && item.baseTypeCode == baseType)?.questions

    let questionAnswerCount = exericseStore.questionAnswers.filter((item) => {
      return know.questions?.includes(item.questionId!) && item.baseTypeCode === baseType
    }).length

    let questionAnswerCorrectCount = exericseStore.questionAnswers.filter((item) => {
      return know.questions?.includes(item.questionId!) && item.baseTypeCode === baseType && item.status === 1
    }).length

    let questionAnswerErrorCount = questionAnswerCount - questionAnswerCorrectCount

    let knowExerciseProgress = knowsExerciseProgress.find(item => item.id == know.id && item.baseTypeCode == baseType)
    if (knowExerciseProgress) {
      knowExerciseProgress.questionAnswerCount = questionAnswerCount
      knowExerciseProgress.questionAnswerCorrectCount = questionAnswerCorrectCount
      knowExerciseProgress.questionAnswerErrorCount = questionAnswerErrorCount
      knowExerciseProgress.answerCorrectRate = Math.round((questionAnswerCorrectCount / questionAnswerCount) * 100)
      if (questionAnswerCount === 0) {
        knowExerciseProgress.skilled = 0
      }
      else if (questionAnswerCount > 0) {
        let questionCount = know.questions!.length
        if (questionAnswerCount == questionCount && knowExerciseProgress.answerCorrectRate > 90) {
          knowExerciseProgress.skilled = 2
        }
        else {
          knowExerciseProgress.skilled = 1
        }
      }
    }
  })
}
/**
 *  添加答题记录
 * @param questionAnswer
 */
export const useAddQuestionAnswer = async (questionAnswer: ExamQuestionAnswerVo) => {
  const exericseStore = useExericseStore()
  exericseStore.addQuestionAnswer(questionAnswer)
  // 设置考点练习进度
  setKnowExerciseProgress(questionAnswer.baseTypeCode!, questionAnswer.knows)
}


/**
 * * 根据答题状态统计试题数量
 * @param baseType
 * @param questionIds
 * @returns
 */
export const useQuestionAnswerCount = (baseType: string, questionIds?: number[], status?: 0 | 1) => {
  const exericseStore = useExericseStore()
  const count = ref<number>(0)

  if (status === 0 || status === 1) {
    count.value = exericseStore.questionAnswers.filter((item) => {
      return questionIds?.includes(item.questionId!) && item.baseTypeCode === baseType && item.status === status
    }).length
  }
  else {
    if (questionIds) {
      count.value = exericseStore.questionAnswers.filter((item) => {
        return questionIds?.includes(item.questionId!) && item.baseTypeCode === baseType
      }).length
    }
    else {
      count.value = exericseStore.questionAnswers.filter((item) => {
        return item.baseTypeCode === baseType
      }).length
    }

  }

  return { count }
}

/**
 * 清空答题记录
 * @param questionIds
 */
export const useClearQuestionAnswer = (questionIds: number[]) => {
  const exericseStore = useExericseStore()
  let questionAnswers = exericseStore.questionAnswers.filter((item) => {
    return !questionIds.includes(item.questionId!)
  })

  exericseStore.questionAnswers = questionAnswers

}

/**
 * 根据试题ID获取答题结果
 * @param questionId
 * @returns
 */
export const useGetAnswerResult = (questionId: number) => {
  let result = -1
  const exericseStore = useExericseStore()
  let answerItem = exericseStore.questionAnswers.find((item) => item.questionId === questionId)
  if (answerItem) {
    result = answerItem.status!
  }

  return result

}

/**
 * 添加试题收藏
 * @param val
 */
export const useQuestionCollect = (val: ExamQuestionCollectVo) => {
  const exericseStore = useExericseStore()
  if (!useIsQuestionCollect(val.questionId!).isQuestionCollect.value) {
    exericseStore.addQuestionCollect(val)

  }
  else {
    exericseStore.removeQuestionCollect(val.questionId!)
  }
}


/**
 *
 * @param val 是否试题已收藏
 */
export const useIsQuestionCollect = (val?: number) => {
  const isQuestionCollect = ref(false)
  const exericseStore = useExericseStore()

  let flag = exericseStore.questionCollects.findIndex((item) => item.questionId == val)
  if (flag >= 0) {
    isQuestionCollect.value = true
  }

  return { isQuestionCollect }
}

/**
 *答题-错题-数量
 * @param baseType
 */
export const useAnswerErrorCount = (baseType: string) => {
  const exericseStore = useExericseStore()
  let count = exericseStore.questionAnswerErrors.filter((item) => item.baseTypeCode === baseType).length
  return count

}


/**
 *答题-未做-数量
 * @param baseType
 */
export const useAnswerNoCount = (baseType: string) => {
  const exericseStore = useExericseStore()
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let qbCount = learnBaseInfoStore.questionCount(baseType)
  let answerCount = exericseStore.questionAnswers.filter((item) => item.baseTypeCode === baseType).length
  return qbCount - answerCount

}

/**
 *答题-收藏题-数量
 * @param baseType
 */
export const useAnswerCollectCount = (baseType: string) => {
  const exericseStore = useExericseStore()
  let count = exericseStore.questionCollects.filter((item) => item.baseTypeCode === baseType).length
  return count
}

/**
 *答题-正确率
 * @param baseType
 */
export const useAnswerCorrectRate = (baseType: string) => {
  const exericseStore = useExericseStore()
  let answerCount = exericseStore.questionAnswers.filter((item) => item.baseTypeCode === baseType).length
  let answerCorrectCount = exericseStore.questionAnswerErrors.filter((item) => item.baseTypeCode === baseType).length

  if (answerCount == 0) {
    return 0
  }
  else {
    return Math.round((answerCorrectCount / answerCount) * 100)

  }

}

/**
   *设置答题位置
   * @param id
   * @param topicIndex
   */
export const useSetAnswerTopicIndex = (id: string, topicIndex: number) => {
  const exericseStore = useExericseStore()
  return exericseStore.setAnswerTopicIndex(id, topicIndex)
}

/**
 *
 * @param baseType 获取练习试题
 * @returns
 */
export const useExerciseQuestions = (options: any) => {
  let exerciseType: ExerciseTypeEnum = parseInt(options.exerciseType)
  let baseType: string = options.baseType
  let code: any

  let questions: LearnBaseQuestionInfo[] | undefined = []
  let answerTopicIndex: AnswerTopicIndex | undefined
  let exericseStore = useExericseStore()
  switch (exerciseType) {
    // 顺序练习
    case ExerciseTypeEnum.SequenceExercise:
      questions = useQuestions(baseType).questions.value
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_sequence_exercise')
      break
    // 顺序练习未做题
    case ExerciseTypeEnum.SequenceExerciseNoAnswerQuestions:
      questions = getSequenceExerciseNoAnswerQuestions(baseType)
      // answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_sequence_exercise_no_answer_questions')
      break
    case ExerciseTypeEnum.SelectedBaseExercise:
      code = parseInt(options.code)
      questions = getSelectedQuestions(baseType, code)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_selected_base_exercise_' + code)
      break
    case ExerciseTypeEnum.SelectedBaseNoExercise:
      questions = getSelectedBaseNoExerciseQuestions(baseType)
      break
    case ExerciseTypeEnum.SelectedBaseErrorQuestionsExercise:
      questions = getSelectedBaseErrorQuestions(baseType)
      break
    case ExerciseTypeEnum.TopicSkillExercise:
      questions = getTopicSkillExercise(baseType, options.questionIds)
      break
    case ExerciseTypeEnum.SpecialExerciseLocateQuestions:
      questions = getSpecialExerciseLocateQuestions(baseType)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_special_exercise_locate_question_topic_index')
      break
    case ExerciseTypeEnum.SequenceExerciseAnsweredQuestions:
      questions = getSequenceExerciseAnsweredQuestions(baseType)
      break
    case ExerciseTypeEnum.SequenceExerciseErrorQuestions:
      questions = getSequenceExerciseErrorQuestions(baseType)
      break
    case ExerciseTypeEnum.SequenceExerciseCollectsQuestions:
      questions = getSequenceExerciseCollectsQuestions(baseType)
      break
    case ExerciseTypeEnum.SpecialExerciseNewRuleQuestions:
      if (baseType === 'km1') {
        code = '1101'
      }
      else {
        code = '4101'
      }
      questions = getSpecialExerciseQuestions(baseType, code!)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_special_exercise_new_rule_question_topic_index_' + code)

      break

    case ExerciseTypeEnum.SpecialExerciseEasyErrorQuestions: //易错题练习
      if (baseType === 'km1') {
        code = '1102'
      }
      else {
        code = '4102'
      }
      questions = getSpecialExerciseQuestions(baseType, code!)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_special_exercise_easy_error_question_topic_index_' + code)

      break
    case ExerciseTypeEnum.SpecialExerciseDisputeQuestions:
      if (baseType === 'km1') {
        code = '1103'
      }
      else {
        code = '4103'
      }
      questions = getSpecialExerciseQuestions(baseType, code!)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_special_exercise_dispute_question_topic_index_' + code)
      break
    case ExerciseTypeEnum.SpecialExerciseMultipleQuestions:
      questions = getSpecialExerciseMultipleQuestions(baseType)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_special_exercise_multiple_question_topic_index')
      break
    case ExerciseTypeEnum.SpecialExerciseQuestions:
      code = options.code
      questions = getSpecialExerciseQuestions(baseType, code!)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_special_exercise_question_topic_index_' + code)
      break
    case ExerciseTypeEnum.ChapterExerciseQuestions:
      code = options.code
      questions = useGetChapterExercisesQuestions(baseType, code!)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_chapter_exercise_question_topic_index_' + code)
      break
    case ExerciseTypeEnum.KnowExerciseQuestions:
      let knowTypeCode = options.knowTypeCode
      let knowId: number = options.knowId
      questions = getKnowExercisesQuestions(baseType, knowTypeCode, knowId)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_know_exercise_question_topic_index_' + knowId)
      exericseStore.setKnowExerciseIndex(baseType, knowTypeCode, knowId)
      break
    case ExerciseTypeEnum.TodayErrorQuestionExercise://今日错题
      questions = getTodayErrorQuestions(baseType)
      break
    case ExerciseTypeEnum.AnswerErrorNumExercise://答错次数-错题练习
      let answerErrorNum = options.code
      questions = getAnswerErrorNumExerciseQuestions(baseType, answerErrorNum)
      break
    case ExerciseTypeEnum.ChapterErrorQuestionExercise://答错次数-错题练习
      let chapterCode = options.code
      questions = getChapterErrorQuestionExerciseQuestions(baseType, chapterCode)
      break
    case ExerciseTypeEnum.KnowErrorQuestionExercise://考点错题-练习
      code = options.code
      questions = getKnowErrorQuestionExerciseQuestions(baseType, code)
      break
    case ExerciseTypeEnum.TodayCollectQuestionExercise://今日收藏题-练习
      questions = getTodayCollectQuestions(baseType)
      break
    case ExerciseTypeEnum.ChapterCollectQuestionExercise://章节收藏练习
      code = options.code
      questions = getChapterCollectQuestionExerciseQuestions(baseType, code)
      break
    case ExerciseTypeEnum.KnowCollectQuestionExercise: //考点收藏练习
      code = options.code
      questions = getKnowCollectQuestionExerciseQuestions(baseType, code)
      break
    case ExerciseTypeEnum.SignQuestionExercise: //标志题藏练习
      let signParentId = options.signParentId
      let signId = options.signId
      questions = useSignQuestions(baseType, signParentId, signId)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_sign_question_exercise_topic_index_' + signId)
      break
    case ExerciseTypeEnum.KnowSummaryQuestionExercise: //考点总结题藏练习
      let knowSummaryId = options.knowSummaryId
      questions = getKnowExerciseQuestions(knowSummaryId)
      answerTopicIndex = exericseStore.getAnswerTopicIndex(baseType + '_know_summary_question_exercise_topic_index_' + knowSummaryId)
      break
    default:
      throw new Error("Unknown ExerciseType=" + exerciseType);
  }

  return {
    questions, answerTopicIndex
  }
}

/**
 * 考点收藏练习-试题
 * @param baseType
 * @param knowId
 * @returns
 */
const getKnowCollectQuestionExerciseQuestions = (baseType: string, knowId: number) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  // 得到错题ID
  let collectQuestionIds = exericseStore.questionCollects.filter(item => item.baseTypeCode == baseType).map(item => item.questionId)

  let knowQuestionIds = exericseStore.knowsExerciseProgress.find(item => item.id == knowId)?.questions

  knowQuestionIds = knowQuestionIds?.filter(item => collectQuestionIds.includes(item))!

  let questions = knowQuestionIds.map(collectQuestionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === collectQuestionId)!
  })

  return questions
}

/**
 * 章节收藏题练习-试题
 * @param baseType
 * @param chapterCode
 * @returns
 */
const getChapterCollectQuestionExerciseQuestions = (baseType: string, chapterCode: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  // 得到错题ID
  let collectQuestionIds = exericseStore.questionCollects.filter(item => item.baseTypeCode == baseType).map(item => item.questionId)
  // 得到章节试题
  let questions = collectQuestionIds.map(collectQuestionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === collectQuestionId)!
  })
  questions = questions.filter(item => item.classesCode === chapterCode)
  return questions
}

/**
 * 考点错题练习-试题
 * @param baseType
 * @param knowId
 * @returns
 */
const getKnowErrorQuestionExerciseQuestions = (baseType: string, knowId: number) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  // 得到错题ID
  let answerErrorQuestionIds = exericseStore.questionAnswerErrors.filter(item => item.baseTypeCode == baseType).map(item => item.questionId)
  let knowQuestionIds = exericseStore.knowsExerciseProgress.find(item => item.id == knowId)?.questions!

  knowQuestionIds = knowQuestionIds?.filter(item => answerErrorQuestionIds.includes(item))

  let questions = knowQuestionIds.map(errorQuestionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === errorQuestionId)!
  })


  return questions
}
/**
 * 章节错题练习-试题
 * @param baseType
 * @param chapterCode
 * @returns
 */
const getChapterErrorQuestionExerciseQuestions = (baseType: string, chapterCode: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  // 得到错题ID
  let answerErrorQuestionIds = exericseStore.questionAnswerErrors.filter(item => item.baseTypeCode == baseType).map(item => item.questionId)
  // 得到章节试题
  let questions = answerErrorQuestionIds.map(errorQuestionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === errorQuestionId)!
  })
  questions = questions.filter(item => item.classesCode == chapterCode)
  return questions
}
/**
 * 答错次数-练习题
 * @param baseType
 * @param answerErrorNum
 */
const getAnswerErrorNumExerciseQuestions = (baseType: string, answerErrorNum: number) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  let answerErrorQuestions = exericseStore.questionAnswerErrors.filter(item => item.baseTypeCode == baseType)
  if (answerErrorNum === 5) {
    answerErrorQuestions = answerErrorQuestions.filter(item => item.errorCount >= answerErrorNum)
  }
  else {
    answerErrorQuestions = answerErrorQuestions.filter(item => item.errorCount == answerErrorNum)
  }

  let questions = answerErrorQuestions.map(errorQuestion => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === errorQuestion.questionId)!
  })
  return questions
}

/**
 *
 * @param selectedBase 精选500题练习进度
 * @returns
 */
export const useSelectedBaseExerciseProcess = (selectedBase: SelectedBase) => {

  if (!selectedBase) return

  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()

  /**
   * 精选题库试题
   */
  let questions: LearnBaseQuestionInfo[] = []
  // 总数数量
  let count = 0
  // 答对数量
  let correctCount = 0
  // 答错数量
  let errorCount = 0
  // 未练习数
  let noExerciseCount = 0
  let questionIds: number[] = []
  selectedBase.classesItems?.forEach(classesItem => {
    questionIds = [...questionIds, ...classesItem.questionIds!]
  })


  questions = learnBaseInfoStore.baseQuestions.filter((questionItem: LearnBaseQuestionInfo) => questionIds.includes(questionItem.questionId!))
  count = questions.length

  correctCount = exericseStore.questionAnswers.filter(item => questionIds.includes(item.questionId!) && item.status === 1).length
  errorCount = exericseStore.questionAnswers.filter(item => questionIds.includes(item.questionId!) && item.status === 0).length
  // 未练习
  noExerciseCount = count - (correctCount + errorCount)

  return {
    count, correctCount, errorCount, noExerciseCount
  }

}

/**
 * 精选试题分类练习进度
 * @param questionIds
 * @returns
 */
export const useSelectedBaseClassesExerciseProcess = (questionIds: number[]) => {
  const exericseStore = useExericseStore()
  // 数量
  let count = 0
  if (!questionIds) return
  count = exericseStore.questionAnswers.filter(item => questionIds.includes(item.questionId!) && item.status === 1).length
  return {
    count
  }
}

/**
 * 获取精选试题
 * @param baseType
 * @param selectedCode
 */
export const getSelectedQuestions = (baseType: string, selectedCode: number) => {
  const selectedBase = useSelectedBase(baseType).value
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let questions = learnBaseInfoStore.baseQuestions.filter((questionItem: LearnBaseQuestionInfo) => selectedBase?.classesItems?.find(classesIitem => classesIitem.code == selectedCode)?.questionIds?.includes(questionItem.questionId!))
  return questions

}

/**
 * 精选题库未练习试题
 * @param baseType
 * @returns
 */
export const getSelectedBaseNoExerciseQuestions = (baseType: string) => {

  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  const selectedBase = useSelectedBase(baseType).value!

  /**
   * 精选题库试题
   */
  let questions: LearnBaseQuestionInfo[] = []

  let questionIds: number[] = []
  selectedBase.classesItems?.forEach(classesItem => {
    questionIds = [...questionIds, ...classesItem.questionIds!]
  })

  questions = learnBaseInfoStore.baseQuestions.filter((questionItem: LearnBaseQuestionInfo) => questionIds.includes(questionItem.questionId!))
  let exerciseQuestionIds = exericseStore.questionAnswers.map(item => item.questionId)

  let noExerciseQuestions = questions.filter((questionItem: LearnBaseQuestionInfo) => !exerciseQuestionIds.includes(questionItem.questionId));
  return noExerciseQuestions
}

/**
 *
 * @param baseType 获取精选题库错题
 * @returns
 */
export const getSelectedBaseErrorQuestions = (baseType: string) => {

  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()
  const selectedBase = useSelectedBase(baseType).value!

  /**
   * 精选题库试题
   */
  let questions: LearnBaseQuestionInfo[] = []

  let questionIds: number[] = []
  selectedBase.classesItems?.forEach(classesItem => {
    questionIds = [...questionIds, ...classesItem.questionIds!]
  })

  questions = learnBaseInfoStore.baseQuestions.filter((questionItem: LearnBaseQuestionInfo) => questionIds.includes(questionItem.questionId!))

  let errorExerciseQuestionIds = exericseStore.questionAnswers.filter(item => item.status === 0).map(item => item.questionId)

  let errorExerciseQuestions = questions.filter((questionItem: LearnBaseQuestionInfo) => errorExerciseQuestionIds.includes(questionItem.questionId));
  return errorExerciseQuestions
}

/**
 * 顺序练习未答题
 * @param baseType
 */
export const getSequenceExerciseNoAnswerQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()

  let answerQuestionIds = exericseStore.questionAnswers.map(item => item.questionId)
  let questions = learnBaseInfoStore.questions(baseType)
    .filter((questionItem: LearnBaseQuestionInfo) => !answerQuestionIds.includes(questionItem.questionId))

  return questions

}

/**
 * 顺序练已答题
 * @param baseType
 */
export const getSequenceExerciseAnsweredQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()

  let answerQuestionIds = exericseStore.questionAnswers.map(item => item.questionId)
  let questions = learnBaseInfoStore.questions(baseType)
    .filter((questionItem: LearnBaseQuestionInfo) => answerQuestionIds.includes(questionItem.questionId))

  return questions

}

/**
 * 顺序练习错题
 * @param baseType
 */
export const getSequenceExerciseErrorQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()

  let errorQuestionIds = exericseStore.questionAnswerErrors.map(item => item.questionId)

  let questions = errorQuestionIds.map(errorQuestionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === errorQuestionId)!
  })
  return questions

}


/**
 * 今日错题
 * @param baseType
 */
export const getTodayErrorQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()

  let date = formatDate(new Date(), 'YYYY-MM-DD')
  let dateBegin = date + ' 00:00:00'
  let dateEnd = date + ' 23:59:59'


  let errorQuestionIds = exericseStore.questionAnswerErrors.filter(item => item.creatTime! >= dateBegin && item.creatTime! <= dateEnd).map(item => item.questionId)
  let questions = errorQuestionIds.map(errorQuestionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId === errorQuestionId)!
  })

  return questions

}


/**
 * 顺序练习-收藏题
 * @param baseType
 */
export const getSequenceExerciseCollectsQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  const exericseStore = useExericseStore()

  let collectQuestionIds = exericseStore.questionCollects.map(item => item.questionId)

  let questions: LearnBaseQuestionInfo[] = collectQuestionIds.map(collectQuestionId => {
    let question = learnBaseInfoStore.baseQuestions.find(item => item.questionId === collectQuestionId)
    return question!
  })

  return questions

}
/**
 * 获取本题技巧相关题
 * @param baseType
 * @param questionIds
 */
export const getTopicSkillExercise = (baseType: string, questionIds: number[]) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let questions = learnBaseInfoStore.questions(baseType).filter((questionItem: LearnBaseQuestionInfo) => questionIds.includes(questionItem.questionId!))
  return questions
}


/**
 * 专项练习统计
 * @param exerciseType
 * @param baseType
 * @param specialCode
 */
export const useSpecialExerciseCount = (exerciseType: ExerciseTypeEnum, baseType: string, specialCode?: string) => {
  let questions: LearnBaseQuestionInfo[] | undefined = []
  let count: number = 0
  // return { count }
  switch (exerciseType) {
    // 顺序练习未做题
    case ExerciseTypeEnum.SequenceExerciseNoAnswerQuestions:
      questions = getSequenceExerciseNoAnswerQuestions(baseType)
      count = questions?.length || 0
      break
    case ExerciseTypeEnum.SequenceExerciseAnsweredQuestions:
      questions = getSequenceExerciseAnsweredQuestions(baseType)
      count = questions?.length || 0
      break
    case ExerciseTypeEnum.SequenceExerciseErrorQuestions:
      questions = getSequenceExerciseErrorQuestions(baseType)
      count = questions?.length || 0
      break
    case ExerciseTypeEnum.SequenceExerciseCollectsQuestions:
      questions = getSequenceExerciseCollectsQuestions(baseType)
      count = questions?.length || 0
      break

    case ExerciseTypeEnum.SpecialExerciseNewRuleQuestions:
      if (baseType === 'km1') {
        specialCode = '1101'
      }
      else {
        specialCode = '4101'
      }
      count = getSpecialExerciseQuestionsCount(baseType, specialCode!) || 0
      break

    case ExerciseTypeEnum.SpecialExerciseEasyErrorQuestions:
      if (baseType === 'km1') {
        specialCode = '1102'
      }
      else {
        specialCode = '4102'
      }
      count = getSpecialExerciseQuestionsCount(baseType, specialCode!) || 0
      break
    case ExerciseTypeEnum.SpecialExerciseDisputeQuestions:
      if (baseType === 'km1') {
        specialCode = '1103'
      }
      else {
        specialCode = '4103'
      }
      count = getSpecialExerciseQuestionsCount(baseType, specialCode!) || 0
      break
    case ExerciseTypeEnum.SpecialExerciseMultipleQuestions:
      questions = getSpecialExerciseMultipleQuestions(baseType)
      count = questions?.length || 0
      break
    case ExerciseTypeEnum.SpecialExerciseLocateQuestions:
      questions = getSpecialExerciseLocateQuestions(baseType)
      count = questions?.length || 0
      break

    default:
      count = 0;

  }

  return {
    count
  }
}

/**
 * 获取专项练习列表
 * @param baseType
 */
export const useGetSpecialExerciseList = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.learnBaseInfo.specialExercises?.filter(specialExercise => specialExercise.baseTypeCode === baseType)
}

/**
 *
 * @param baseType 获取专项练习的试题
 * @param specialCode
 */
export const getSpecialExerciseQuestions = (baseType: string, specialCode: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let questionIds = learnBaseInfoStore.learnBaseInfo.specialExercises?.find(specialExercise => specialExercise.code === specialCode)?.questions
  return learnBaseInfoStore.questions(baseType).filter((questionItem: LearnBaseQuestionInfo) => questionIds?.includes(questionItem.questionId!))
}


/**
 *
 * @param baseType 获取专项练习试题统计
 * @param specialCode
 */
export const getSpecialExerciseQuestionsCount = (baseType: string, specialCode: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let questionIds = learnBaseInfoStore.learnBaseInfo.specialExercises?.find(specialExercise => specialExercise.code === specialCode)?.questions
  return questionIds?.length
}
/**
 * 专项练习地方题
 * @param baseType
 */
export const getSpecialExerciseLocateQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let cityCode = learnBaseInfoStore.learnBaseInfo.cityInfo?.code
  if (cityCode) {
    let regionCode: any = cityCode.substring(0, 2)
    let locateClasses = learnBaseInfoStore.learnBaseInfo.classes?.find(item => item.baseType == baseType && item.regionCode == regionCode)
    if (locateClasses) {
      let questions = learnBaseInfoStore.questions(baseType).filter((item: LearnBaseQuestionInfo) => item.classesCode === locateClasses.code)
      return questions
    }
  }

  return []
}

/**
 * 多选题
 * @param baseType
 */
const getSpecialExerciseMultipleQuestions = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.questions(baseType).filter((item: LearnBaseQuestionInfo) => item.optionType! == '2')

}

/**
 * 检查是否有本地试题
 * @param baseType
 */
export const useLocateQuestionsCheck = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let cityCode = learnBaseInfoStore.learnBaseInfo.cityInfo?.code
  if (cityCode) {
    let regionCode: any = cityCode.substring(0, 2)
    let locateClasses = learnBaseInfoStore.learnBaseInfo.classes?.find(item => item.baseType == baseType && item.regionCode == regionCode)
    if (locateClasses) {
      return true
    }
  }

  return false
}


/**
 * 专项练习章节列表
 * @param baseType
 */
export const useSpecialExerciseChapterList = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.learnBaseInfo.classes?.filter(item => item.baseType == baseType)
}

/**
 *
 * @param baseType 获取考点数量
 * @returns
 */
export const useSpecialExerciseKnowCount = (baseType: string) => {
  const exerciseStore = useExericseStore()
  let count = exerciseStore.knowsExerciseProgress.filter(item => item.baseTypeCode === baseType).length
  return count
}

/**
 * 获取章节练习
 * @param baseType
 */
export const useGetChapterExercises = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.learnBaseInfo.classes?.filter(item => item.baseType === baseType)

}



/**
 *获取章节练习试题
 * @param baseType
 * @param code
 * @returns
 */
export const useGetChapterExercisesQuestions = (baseType: string, code: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.questions(baseType).filter((item: LearnBaseQuestionInfo) => item.classesCode === code)
}

/**
 *
 * @param baseType 获取知识点练习
 */
export const useGetKnowExercises = (baseType: string) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  return learnBaseInfoStore.learnBaseInfo.knowExercises?.filter(item => item.baseTypeCode?.includes(baseType))
}

/**
 *
 * @param baseType 获取考点练习
 */
export const useGetKnowExercise = (baseType: string, knowTypeCode: string) => {
  const exerciseStore = useExericseStore()
  return exerciseStore.knowsExerciseProgress.filter(item => item.baseTypeCode == baseType && item.knowTypeCode === knowTypeCode)
}


/**
 * 获取考点练习题
 * @param baseType
 * @param knowTypeCode
 * @param knowId
 */
export const getKnowExercisesQuestions = (baseType: string, knowTypeCode: string, knowId: number) => {
  const learnBaseInfoStore = useLearnBaseInfoStore()
  let questionIds = learnBaseInfoStore.learnBaseInfo.knowExercises?.find(item => item.code == knowTypeCode)?.knows?.find(know => know.id == knowId)?.questions
  let questions = questionIds?.map(questionId => {
    return learnBaseInfoStore.baseQuestions.find(item => item.questionId == questionId)!
  })

  questions = questions!.filter(item => item?.baseTypeCode == baseType)
  return questions
}


